package com.ibeeking.gmc.global.g.rest.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.gmc.global.g.rest.service.IShortUrlService;
import com.ibeeking.gmc.global.service.common.param.ShortUrlParam;
import com.ibeeking.gmc.global.service.common.query.ShortUrlPageQuery;
import com.ibeeking.gmc.global.service.common.query.ShortUrlQuery;
import com.ibeeking.gmc.global.service.common.vo.ShortUrlVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/g/shortUrl/v1")
public class ShortUrlController {

    @Resource
    private IShortUrlService shortUrlServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "列表查询")
    @PostMapping(value = "/list")
    public List<ShortUrlVO> list(@RequestBody ShortUrlQuery query) {
        return shortUrlServiceImpl.list(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询列表")
    @PostMapping(value = "/pageList")
    public Page<ShortUrlVO> pageList(@RequestBody ShortUrlPageQuery pageQuery) {
        return this.shortUrlServiceImpl.pageList(pageQuery);
    }

    @Log(logType = OperateLogTypeEnum.QUERY,describe = "查询")
    @PostMapping(value = "/view/{id}")
    public ShortUrlVO view(@PathVariable("id") Long id) {
        return this.shortUrlServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.SAVE,describe = "新增")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser,@RequestBody @Validated(BaseValidationGroup.Insert.class) ShortUrlParam param){
        param.setCreateBy(currentUser.getId());
        param.setModifyBy(currentUser.getId());
        return this.shortUrlServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) ShortUrlParam param){
        param.setModifyBy(currentUser.getId());
        return this.shortUrlServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.DEL,describe = "删除")
    @PostMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id){
        return this.shortUrlServiceImpl.del(id);
    }
}
